package cn.kxx.serviceImpl;

import cn.kxx.config.DateUtil;
import cn.kxx.constant.RedisConstant;
import cn.kxx.exception.BusinessException;
import cn.kxx.mapper.MemberMapper;
import cn.kxx.mapper.OrderMapper;
import cn.kxx.mapper.OrderSettingMapper;
import cn.kxx.pojo.Member;
import cn.kxx.pojo.Order;
import cn.kxx.pojo.OrderSetting;
import cn.kxx.service.OrderService;
import cn.kxx.vo.OrderInfoVo;
import cn.kxx.vo.OrderInfoVo2;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public int submit(OrderInfoVo orderInfoVo) throws BusinessException {

//        1、校验验证码，是否过期，是否正确
        if (orderInfoVo == null){
            throw new BusinessException("预约信息为空");
        }
        String telephone = orderInfoVo.getTelephone();
        if (StringUtils.isEmpty(telephone)){
            throw new BusinessException("手机号为空");
        }

        Object o = redisTemplate.opsForValue().get(RedisConstant.SENDTYPE_ORDER + ":" + telephone);
        String code = String.valueOf(o);
        if (StringUtils.isEmpty(code)||code==null){
            throw new BusinessException("验证码过期");
        }
        if (!code.equals(orderInfoVo.getValidateCode())){
            throw new BusinessException("验证码错误");
        }
//        2、检查用户所选择的预约日期是否已经进行了预约设置，如果没有设置则无法进行预约

        Date orderDate = orderInfoVo.getOrderDate();
        OrderSetting orderSettingByOrderDate = orderSettingMapper.getOrderSettingByOrderDate(orderDate);
        if (orderSettingByOrderDate == null){
            throw new BusinessException("当前日期不能预约");
        }

//        3、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
//
        int number = orderSettingByOrderDate.getNumber();
        int reservations = orderSettingByOrderDate.getReservations();
        if (reservations>=number){
            throw new BusinessException("预约已满");
        }
//        4、检查当前用户是否为会员，如果不是会员则自动完成注册会员并进行预约
        Member member = memberMapper.selectOne(new LambdaQueryWrapper<Member>().eq(Member::getPhoneNumber,telephone));
        if (member==null){
            member = new Member();
            member.setName(orderInfoVo.getName());
            member.setSex(orderInfoVo.getSex());
            member.setPhoneNumber(telephone);
            member.setIdCard(orderInfoVo.getIdCard());
            member.setRegTime(new Date());
            memberMapper.insert(member);
        }

//        5、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
//
        Integer memberId = member.getId();
        String dateStr = DateUtil.dateToStr(orderInfoVo.getOrderDate(), "yyyy-MM-dd");
        Integer setmealId = orderInfoVo.getSetmealId();
        Order order = orderMapper.selectOne(
                new LambdaQueryWrapper<Order>().eq(Order::getMemberId, member)
                        .eq(Order::getOrderDate, dateStr)
                        .eq(Order::getSetmealId, setmealId)
        );
        if (order!=null)
            throw new BusinessException("此套餐已经预约");
//        6、更新当日的已预约人数
        orderSettingByOrderDate.setReservations(++reservations);
        orderSettingMapper.updateById(orderSettingByOrderDate);
//        7、保存预约信息到预约表
        order = new Order();
        order.setMemberId(memberId);
        order.setOrderType(orderInfoVo.getOrderType());
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderDate(orderDate);
        order.setSetmealId(setmealId);
        orderMapper.insert(order);
        return order.getId();
    }


    @Override
    public OrderInfoVo2 findById(Integer id) throws BusinessException {
        return orderMapper.findById(id);
    }
}
